//
//  SecondViewController.m
//  MemoryTravel
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 fcx. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *foreignCV;
@property (nonatomic, retain) NSMutableArray *AsiaArray;
@property (nonatomic, retain) NSMutableArray *EuropeArray;
@property (nonatomic, retain) NSMutableArray *othersArray;
@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.AsiaArray = [NSMutableArray array];
    self.EuropeArray = [NSMutableArray array];
    self.othersArray = [NSMutableArray array];
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(160 , 230 );
    flowLayout.minimumLineSpacing = 20 ;
    flowLayout.minimumInteritemSpacing = 0.5 ;
    flowLayout.sectionInset = UIEdgeInsetsMake(20 , 20 , 20 , 20 );
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.foreignCV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, Width, Height - 140) collectionViewLayout:flowLayout];
    self.foreignCV.backgroundColor = [UIColor whiteColor];
    
    self.foreignCV.delegate = self;
    self.foreignCV.dataSource = self;
    
    [self.foreignCV registerClass:[DBYInlandCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.foreignCV registerClass: [FirstCollectionReusableView class]forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    
    [self.view addSubview:self.foreignCV];
    
    [self getAFNetWorkingData];
    
    // Do any additional setup after loading the view.
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.AsiaArray.count;
    }else if (section == 1){
        return self.EuropeArray.count;
    }else{
        return self.othersArray.count;
        
    }
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DBYInlandCollectionViewCell *foreignCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    WorldModel *foreignModel = [[WorldModel alloc] init];
    if (indexPath.section == 0) {
        foreignModel = [self.AsiaArray objectAtIndex:indexPath.item];
    }else if (indexPath.section == 1){
        foreignModel = [self.EuropeArray objectAtIndex:indexPath.item];
    }else{
        foreignModel = [self.othersArray objectAtIndex:indexPath.item];
    }
    foreignCell.aModel = foreignModel;
    return foreignCell;
    
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(Width, 30 );
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        FirstCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        if (indexPath.section == 0) {
            headView.label.text = @"亚洲";
        }else if (indexPath.section == 1){
            headView.label.text = @"欧洲";
        }else{
            headView.label.text = @"美洲,大洋洲,非洲与南极洲";
        }
        headView.label.textAlignment =  NSTextAlignmentCenter;
        return headView;
    }
    
    return nil;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    WorldDetailViewController *foreignDetailVC = [[WorldDetailViewController alloc] init];
    
    foreignDetailVC.hidesBottomBarWhenPushed = YES;
    if (indexPath.section == 0) {
        foreignDetailVC.aModel = [self.AsiaArray objectAtIndex:indexPath.row];
    }else if (indexPath.section == 1){
        foreignDetailVC.aModel = [self.EuropeArray objectAtIndex:indexPath.row];
    }else
    {
        foreignDetailVC.aModel = [self.othersArray objectAtIndex:indexPath.row];
    }
    
    [self.navigationController pushViewController:foreignDetailVC animated:YES];
    
}



- (void)getAFNetWorkingData{
    NSString *url_string = @"http://chanyouji.com/api/destinations.json";
    [AppTools getDataWithUrlStr:url_string andBlock:^(id result) {
        
        
        
        //        [self.AsiaArray removeAllObjects];
        //        [self.EuropeArray removeAllObjects];
        //        [self.othersArray removeAllObjects];
        
        NSMutableArray *bigArray = [NSMutableArray arrayWithArray:result];
        for (NSMutableDictionary *bigDic in bigArray) {
            NSString *count = [bigDic objectForKey:@"category"];
            NSMutableArray *destinationsArray = [bigDic objectForKey:@"destinations"];
            
            
            for (NSMutableDictionary *dic in destinationsArray) {
                if ([count isEqualToString:@"1"]) {
                    WorldModel *foreignModel = [[WorldModel alloc] initWithDictionary:dic];
                    [self.AsiaArray addObject:foreignModel];
                }else if ([count isEqualToString:@"2"]){
                    WorldModel *foreignModel = [[WorldModel alloc] initWithDictionary:dic];
                    [self.EuropeArray addObject:foreignModel];
                }else if ([count isEqualToString:@"3"]){
                    WorldModel *foreignModel = [[WorldModel alloc] initWithDictionary:dic];
                    [self.othersArray addObject:foreignModel];
                }
            }
            
        }
        [self.foreignCV reloadData];
        
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
